<?php

namespace pay\modules\frontend\models;
use Yii;

/**
 * This is the model class for table "pay_configuration".
 *
 * @property int $id
 * @property int $app_id app_id
 * @property int $cid 渠道id
 * @property string $channel_mer_id 渠道商户id
 * @property string $status 1-启用，2-禁用
 * @property string|null $remake 备注
 * @property string|null $configuration 支付配置
 * @property string $create_time
 */
class Configuration extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_configuration';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['app_id', 'cid', 'channel_mer_id', 'create_time'], 'required'],
            [['app_id', 'cid'], 'integer'],
            [['status'], 'string'],
            [['create_time'], 'safe'],
            [['channel_mer_id'], 'string', 'max' => 32],
            [['remake'], 'string', 'max' => 255],
            [['configuration'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'app_id' => 'App ID',
            'cid' => 'Cid',
            'channel_mer_id' => 'Channel Mer ID',
            'status' => 'Status',
            'remake' => 'Remake',
            'configuration' => 'Configuration',
            'create_time' => 'Create Time',
        ];
    }


    public function Configuration($merId)
    {
        $data = self::find()->where('channel_mer_id = :mid and status = :sta',[':mid' => $merId,':sta' => 1])->select(['configuration','channel_mer_id'])->asArray()->one();
        if (!empty($data['configuration'])){
            $data['configuration'] = json_decode($data['configuration'],true);
        }else{
            $data['configuration'] = '';
        }
        return $data['configuration'];
    }
}
